cmake_minimum_required(VERSION 3.10)

if(DEFINED ENV{VCPKG_DEFAULT_TRIPLET})
  message(STATUS "Setting default vcpkg target triplet to $ENV{VCPKG_DEFAULT_TRIPLET}")
  set(VCPKG_TARGET_TRIPLET $ENV{VCPKG_DEFAULT_TRIPLET})
endif()

if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
  set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
  set(X_VCPKG_APPLOCAL_DEPS_INSTALL ON)
  message(STATUS "VCPKG found: $ENV{VCPKG_ROOT}")
  message(STATUS "Using VCPKG integration")
  set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
  if(VCPKG_MANIFEST_FEATURES)
    message(STATUS "VCPKG_MANIFEST_FEATURES: ${VCPKG_MANIFEST_FEATURES}")
  endif()
elseif(DEFINED CMAKE_TOOLCHAIN_FILE)
  message(STATUS "Using toolchain: ${CMAKE_TOOLCHAIN_FILE}")
  if(CMAKE_TOOLCHAIN_FILE MATCHES "vcpkg.cmake")
    message(STATUS "Toolchain uses VCPKG integration")
    if(VCPKG_MANIFEST_FEATURES)
      message(STATUS "VCPKG_MANIFEST_FEATURES: ${VCPKG_MANIFEST_FEATURES}")
    endif()
  endif()
else()
  message(WARNING "vcpkg not found, toolchain not defined, build might fail")
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg.json)
  file(READ ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg.json VCPKG_JSON_STRING)
  string(JSON VERSION_STRING GET ${VCPKG_JSON_STRING} version)
else()
  set(VERSION_STRING "0.0.0")
endif()

string(REPLACE "." ";" VERSION_LIST ${VERSION_STRING})
list(LENGTH VERSION_LIST VERSION_LIST_LENGTH)
if(VERSION_LIST_LENGTH LESS 2)
  message(FATAL_ERROR "Tool needs at least major.minor version numbers to properly configure")
endif()
list(GET VERSION_LIST 0 TOOL_MAJOR_VERSION)
list(GET VERSION_LIST 1 TOOL_MINOR_VERSION)
if(VERSION_LIST_LENGTH GREATER 2)
  list(GET VERSION_LIST 2 TOOL_PATCH_VERSION)
else()
  set(TOOL_PATCH_VERSION 0)
endif()
if(VERSION_LIST_LENGTH GREATER 3)
  list(GET VERSION_LIST 3 TOOL_TWEAK_VERSION)
else()
  set(TOOL_TWEAK_VERSION 0)
endif()

set(TOOL_VERSION ${TOOL_MAJOR_VERSION}.${TOOL_MINOR_VERSION}.${TOOL_PATCH_VERSION}.${TOOL_TWEAK_VERSION})
message(STATUS "TOOL_VERSION: ${TOOL_VERSION}")

project(UltraVNC VERSION ${TOOL_VERSION})
enable_language(CXX)
enable_language(C)
if(WIN32)
  enable_language(RC)
endif()

set(CMAKE_CXX_STANDARD 14)
if (MINGW)
    set(CMAKE_C_FLAGS "-Wno-write-strings")
    set(CMAKE_CXX_FLAGS "-Wno-write-strings -Wno-conversion-null")
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    add_compile_definitions("_CRT_SECURE_NO_WARNINGS")
    add_compile_definitions("_WINSOCK_DEPRECATED_NO_WARNINGS")
    #add_compile_definitions("NOMINMAX")
    set(CMAKE_MFC_FLAG 2)
endif()
if( ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64" )
    add_compile_definitions("_X64")
endif()

option(asan "Build with address sanitizer enabled.")
if (asan)
    add_compile_options(-fsanitize=address)
    add_link_options(-fsanitize=address)
    install(FILES "c:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/clang_rt.asan_dynamic-x86_64.dll" DESTINATION ${CMAKE_BINARY_DIR}/ultravnc OPTIONAL)
endif()

if (MINGW)
    install(FILES /usr/lib/gcc/x86_64-w64-mingw32/13-win32/libstdc++-6.dll DESTINATION ${CMAKE_BINARY_DIR}/ultravnc OPTIONAL)
    install(FILES /usr/lib/gcc/x86_64-w64-mingw32/13-win32/libgcc_s_seh-1.dll DESTINATION ${CMAKE_BINARY_DIR}/ultravnc OPTIONAL)
endif()

add_subdirectory(authadmin)
add_subdirectory(authSSP)
add_subdirectory(createpassword)
add_subdirectory(ldapauth)
add_subdirectory(ldapauth9x)
add_subdirectory(ldapauthnt4)
add_subdirectory(libomnithread)
add_subdirectory(librdr)
#add_subdirectory(libudt4)
#add_subdirectory(libudtcloud)
add_subdirectory(libzip32)
add_subdirectory(libzipunzip)
add_subdirectory(logging)
add_subdirectory(logmessages)
add_subdirectory(MSLogonACL)
add_subdirectory(repeater)
#add_subdirectory(SecureVNCPlugin) # Not publicly available.
add_subdirectory(setcad)
add_subdirectory(setpasswd)
add_subdirectory(testauth)
add_subdirectory(uvnc_settings)
add_subdirectory(uvnckeyboardhelper)
add_subdirectory(vnchooks)
add_subdirectory(vncviewer)
#add_subdirectory(winvnc)
add_subdirectory(workgrpdomnt4)
